package datastructure.deque;

import java.util.Iterator;

/**
 * 双向环形链表带哨兵实现双端队列
 *
 * @author 杨 强
 * @package datastructure.deque
 * @createTime 2023-03-15 21:21
 */
public class LinkedListDeque<E> implements Deque<E>, Iterable<E> {

    public static void main(String[] args) {
        LinkedListDeque<Integer> deque = new LinkedListDeque<>(5);
        deque.offerFirst(1);
        deque.offerFirst(2);
        deque.offerLast(3);
        deque.offerLast(4);
        // 1 2 3 4
        Iterator<Integer> iterator = deque.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    @Override
    public boolean offerFirst(E e) {
        // 判断是满了
        if (isFull()){
            return false;
        }
        // 添加节点的上一个节点
        Node<E> before = sentinel;
        // 添加节点的下一个节点
        Node<E> after = sentinel.next;
        // 添加节点
        Node<E> add = new Node<>(before, e, after);
        // 变换指针
        before.next = add;
        after.prev = add;
        // 元素加一
        size++;
        return true;
    }

    @Override
    public boolean offerLast(E e) {
        // 判断是满了
        if (isFull()){
            return false;
        }
        // 添加节点的上一个节点
        Node<E> before = sentinel.prev;
        // 添加节点的下一个节点
        Node<E> after = sentinel;
        // 添加节点
        Node<E> add = new Node<>(before, e, after);
        // 变换指针
        before.next = add;
        after.prev = add;
        // 元素加一
        size++;
        return true;
    }

    @Override
    public E pollFirst() {
        // 判断是否为空
        if (isEmpty()){
            return null;
        }
        // 头节点的上一个节点
        Node<E> before = sentinel;
        // 需要移除的节点
        Node<E> remove = sentinel.next;
        // 头节点的下一个节点
        Node<E> after = remove.next;
        // 指针移动
        before.next = after;
        after.prev = before;
        // 元素减一
        size--;
        return remove.value;
    }

    @Override
    public E pollLast() {
        if (isEmpty()) {
            return null;
        }
        Node<E> polled = sentinel.prev;
        Node<E> a = polled.prev;
        Node<E> b = sentinel;
        a.next = b;
        b.prev = a;
        size--;
        return polled.value;
    }

    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.next.value;
    }

    @Override
    public E peekLast() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.prev.value;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capacity;
    }

    /**
     * 容量
     */
    private int capacity;
    /**
     * 元素个数
     */
    private int size;
    /**
     * 头尾节点
     */
    private Node<E> sentinel = new Node<>(null, null, null);

    /**
     * 构造器,初始化容量和初始化双向环形带哨兵节点
     * @param capacity 容量
     */
    public LinkedListDeque(int capacity) {
        this.capacity = capacity;
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
    }

    /**
     * 定义节点类
     */
    private static class Node<E>{
        Node<E> prev;
        E value;
        Node<E> next;
        public Node(Node<E> prev, E value, Node<E> next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p = sentinel.next;
            @Override
            public boolean hasNext() {
                return p != sentinel;
            }

            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }
}
